<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
  <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
  <link rel="stylesheet" type="text/css" href="./css/aui.css" />
  <link rel="stylesheet" type="text/css" href="./css/styleold.css" />
  <link rel="stylesheet" type="text/css" href="./css/style.css" />
  <link rel="stylesheet" type="text/css" href="./css/aui-slide.css" />
  <script type="text/javascript" src="./script/base.js"></script>
  <script type="text/javascript" src="./script/api.js"></script>
  <script type="text/javascript" src="./script/jquery.min.js"></script>
</head>
<body>
<!--   <div class="aui-tips aui-margin-b-15" id="tips-1">
    <i class="aui-iconfont aui-icon-info"></i>
    <div class="aui-tips-title" tapmode onclick="index()">点击可直接进入首页</div>
    <i class="aui-iconfont aui-icon-close" tapmode onclick="closeTips()"></i>
  </div> -->
  <header class="aui-bar aui-bar-nav">
    <a class="aui-pull-left" onclick="closeWin()">
      <span class="aui-iconfont aui-icon-left"></span>
    </a>
    <div class="aui-title">注册</div>
  </header>
  <div class="aui-bar-nav-d"></div>

  <ul class="register-t">
    <li class="active">
      <i></i>
      <p>手机注册</p>
    </li>
    <span class="active"></span>
    <li>
      <i></i>
      <p>银行卡认证</p>
    </li>
    <span></span>
    <li>
      <i></i>
      <p>开通银行存管</p>
    </li>
  </ul>

  <ul class="list">
   <li>
     <img src="image/register-icon-5.png">
     <input class="text-1" type="text" placeholder="请输入您的昵称" id="user_name">
   </li>
   <div class="error" id="checkNickName" style="display:none;">昵称不能为空！</div>

   <li>
     <img src="image/register-icon-1.png">
     <input class="text-1" type="text" placeholder="请输入您的手机号码" id="mobile">
   </li>
   <div class="error" id="checkPhoneNumber" style="display:none;">手机号不能为空！</div>

   <li>
     <img src="image/register-icon-2.png">
     <input class="text-2" type="text" placeholder="请输入验证码" id="sms_code" onkeyup="checkCaptcha();">
     <button class="btn-1" onclick="sendCaptcha();" id="sendCodeBtn">获取验证码</button>
   </li>
   <div class="error" id="checkCaptcha" style="display:none;"></div>

 </ul>
 <ul class="list">
   <li>
     <img src="image/register-icon-3.png">
     <input class="text-1" type="password" placeholder="请输入登录密码" id="user_pwd">
   </li>
   <div class="error" id="checkPSW" style="display:none;"></div>

   <li>
     <img src="image/register-icon-3.png">
     <input class="text-1" type="password" placeholder="请再次输入登录密码" id="user_pwd_confirm">
   </li>
   <div class="error" id="checkPSW_2" style="display:none;"></div>

   <!-- <div class="error">
     密码长度要求在6~16位字符之间，密码设置为数字或字母（区分大小
     写），不能包含特殊符号
   </div> -->
   <li>
     <img src="image/register-icon-4.png">
     <input class="text-1" type="text" placeholder="邀请码（选填）" id="referer">
   </li>
   <li>
     <img src="image/register-icon-5.png">
     <input class="text-1" type="text" placeholder="客户经理ID（选填）" id="manager">
   </li>
 </ul>

 <div class="register-d"  tapmode>
  <button onclick="register()">立即注册</button>
  <a onclick="login()">已有账号,去登录</a>
</div>


</body>
<script type="text/javascript" src="./script/api.js"></script>
<script type="text/javascript">

  apiready = function(){
    // 初始化验证码
    lastCaptcha = '';
  }


    // 注册
    function register(){

        // // 用户昵称
        var user_name = $api.byId('user_name');
        var user_nameValue = user_name.value;
        // 手机号
        var mobile = $api.byId('mobile');
        var mobileValue = mobile.value;
        // 验证码
        var sms_code = $api.byId('sms_code');
        var sms_codeValue = sms_code.value;
        // 密码
        var user_pwd = $api.byId('user_pwd');
        var user_pwdValue = user_pwd.value;
        // 确认密码
        var user_pwd_confirm = $api.byId('user_pwd_confirm');
        var user_pwd_confirmValue = user_pwd_confirm.value;
        // 邀请码
        var referer = $api.byId('referer');
        var refererValue = referer.value;
        // 经理ID
        var manager = $api.byId('manager');
        var managerValue = manager.value;

        // 昵称不能为空
        if (!user_nameValue) {
          $('#checkNickName').show();
          return false;
        }else{
          $('#checkNickName').hide();

        }

        // 手机号不能为空
        if (!mobileValue) {
          $('#checkPhoneNumber').show();

          return false;
        }else{
          $('#checkPhoneNumber').hide();
        }

        // 验证码不能为空
        if (!sms_codeValue) {
          $('#checkCaptcha').show();

          $api.byId('checkCaptcha').innerText = '验证码不能为空';
          return false;
        }else{
          $('#checkCaptcha').hide();

          $api.byId('checkCaptcha').innerText = '';
        }

        // 验证验证码
        if (sms_codeValue != lastCaptcha) {
          $('#checkCaptcha').show();

          $api.byId('checkCaptcha').innerText = '验证码错误';
          return false;
        }else{
          $('#checkCaptcha').hide();
          $api.byId('checkCaptcha').innerText = '';
        }

        // 密码不能为空
        if (!user_pwdValue) {

          $api.byId('checkPSW').innerText = '密码不能为空';
          $('#checkPSW').show();

          return false;
        }else{
          $('#checkPSW').hide();
          $api.byId('checkPSW').innerText = '';
        }

        // 再次输入密码不能为空
        if (!user_pwd_confirmValue) {
          $api.byId('checkPSW_2').innerText = '请再次输入密码';
          $('#checkPSW_2').show();
          return false;
        }else{
            // 两次密码输入是否相同
            if (user_pwdValue != user_pwd_confirmValue) {
              $api.byId('checkPSW_2').innerText = '两次密码输入不一致';
              $('#checkPSW_2').show();

              return false;
            }else{
              $('#checkPSW_2').hide();

              $api.byId('checkPSW_2').innerText = '';
            }
          }

          api.ajax({
            url: webAddress + '/index.php?ctl=app_user_center&act=doregister',
            method: 'post',
            data: {
              values: {
                user_group: 0,
                user_name: user_nameValue,
                mobile: mobileValue,
                sms_code: sms_codeValue,
                user_pwd: user_pwdValue,
                user_pwd_confirm: user_pwd_confirmValue,
                referer: refererValue,
                manager: managerValue,
              },
            }
          },function(ret, err){
            if (ret) {
              // alert($api.jsonToStr(ret));
              if (ret.status == 0) {
                alert(ret.info);
                return false;
              }else{
                var userid = ret.userid;
                $api.setStorage('userid', userid);
                // 注册成功后的逻辑
                api.toast({
                  msg: '注册成功',
                  duration: 2000,
                  location: 'middle'
                });
                api.openWin({
                  name: 'register_attestation',
                  url: './register_attestation.html',
                  pageParam: {
                    backUrl: 'login',
                  }
                });
              }
            } else {

              alert(err.msg);
            }
          });




        };

    // 发送验证码
    function sendCaptcha(){

      var mobile = $api.byId('mobile');
      var mobileValue = mobile.value;

      checkMobile(mobileValue);
    };

    // 获取验证码
    function checkMobile(mobileValue) {

      var re = /^1\d{10}$/
      if (re.test(mobileValue)) {

            // 手机号码符合要求后开始倒计时
            var time = 60;
            var sendCodeBtn = $api.byId('sendCodeBtn');
            var timer = setInterval(function(){
              time--;
              $api.removeCls(sendCodeBtn, 'active');
              sendCodeBtn.innerText = time + " 秒";
              sendCodeBtn.removeAttribute('onclick');
              if (time==0) {
                time = 60;
                clearInterval(timer);
                $api.addCls(sendCodeBtn, 'active');
                sendCodeBtn.innerText = "发送验证码";
                sendCodeBtn.setAttribute('onclick',"sendCaptcha();");
              }
            },1000);
            api.ajax({
              url: webAddress + '/index.php?ctl=app_user_center&act=get_register_verify_code_mobile',
              method: 'post',
              data: {
                values: {
                  user_mobile: mobileValue,
                },
              }
            },function(ret, err){
              if (ret) {

                if (ret.status == 0) {
                  alert(ret.info);
                  return false;
                }else{
                  var captcha = ret.code;
                  captchaSend(mobileValue,captcha);
                }
              } else {

                alert(err.msg);
              }
            });
          } else {
            alert("请输入正确的手机号");
            var mobile = $api.byId('mobile');
            mobile.focus();
          }
        }

    // 发送验证码
    function captchaSend(mobileValue,captcha){
        // 将最终收到的验证码定义成全局变量
        lastCaptcha = captcha;

        api.ajax({
          url: webAddress + '/sendmsg/SendTemplateSMS.php',
          method: 'post',
          data: {
            values: {
              mobile: mobileValue,
              code: captcha,
            },
          }
        },function(ret, err){
          if (ret) {
          } else {
                // 逻辑在这里写
                var sms_code = $api.byId('sms_code');
                sms_code.focus();
              }
            });
      };

    // 校验验证码
    function checkCaptcha(){

      var sms_codeEle = $api.byId('sms_code');
      var sms_codeValue = sms_code.value;
      var sms_codeLength = sms_codeValue.length;
      if (sms_codeLength == 6) {
        if (sms_codeValue == lastCaptcha) {
          $api.byId('checkCaptcha').innerText = '验证码正确';
          $('#checkCaptcha').hide();

        }else{
          $api.byId('checkCaptcha').innerText = '验证码错误';
        }
      }else{
        $api.byId('checkCaptcha').innerText = '验证码错误';
      }
    }


    function register_attestation(){
      var delay = 0;
      if(api.systemType != 'ios'){
            // delay = 300;
          }
          api.openWin({
            name: 'register_attestation',
            url: './register_attestation.html',
            bounces:false,
            delay: delay,
            slidBackEnabled:true,
            vScrollBarEnabled:false,
            progress:{
              type:"page"
            }
          });
        };

        function login(){
          var delay = 0;
          if(api.systemType != 'ios'){
            // delay = 300;
          }
          api.openWin({
            name: 'login',
            url: './login.html',
            bounces:false,
            delay: delay,
            slidBackEnabled:true,
            vScrollBarEnabled:false,
            progress:{
              type:"page"
            }
          });
        };
    // 点击直接进入首页
    function closeTips(){
      $api.remove($api.byId("tips-1"));
    }

    function index(){
      var delay = 0;
      if(api.systemType != 'ios'){
            // delay = 300;
          }
          api.openWin({
            name: 'index',
            url: './index.html',
            bounces:false,
            delay: delay,
            slidBackEnabled:true,
            vScrollBarEnabled:false,
            progress:{
              type:"page"
            }
          });
        };

        function closeWin(){
          api.closeWin({
          });
        }
      </script>
      </html>
